/*
 * lib2Desa - Another 2D game library in C++
 * Copyright (C) 2009 Erivaldo Xavier de Lima Filho
 *
 * This program is free software and is distributed under GNU General Public License version 3. You can modify and/or redistribute
 * it under the terms of GPL version 3 or posterior version. This program comes with NO WARRANTY, for more details, see LICENSE,
 * or <http://www.gnu.org/licenses>.
 *
 * Contact info:
 *
 *	Erivaldo Xavier de Lima Filho, desadoc@gmail.com
 *
 */

#include "Ext/SimpleChar.hpp"
#include "Core/Event.hpp"
#include "Core/KeyMessage.hpp"
#include "Core/CollisionMessage.hpp"

#include <iostream>
using namespace std;

#ifndef _NO_NAMESPACE
namespace lib2Desa {
#endif

bool SimpleChar::run(u32 ml)
{
	GameObject::run(ml);

	if(up&&jump) upVel += 0.15;
	else upVel += 0.2;

	upVel = (upVel > 6)? 6: upVel;

	setRelPos(getRelPos() + vector<2,float>(2*leftRight,upVel*((float)ml/16)));

	return true;
}

void SimpleChar::notify(shared_ptr<IEvent> e)
{
	if(e->getType() == IEvent::ET_KEY)
	{
		//Event<KeyMessage>* ke = (Event<KeyMessage>*) ((IEvent*)e);
		shared_ptr<Event<KeyMessage> > ke = e;
		switch(ke->getMessage().getType())
		{
			case KeyMessage::KMT_KEYDOWN :
			{
				switch(ke->getMessage().getKey())
				{
					case KeyMessage::KMK_RIGHT :
					{
						leftRight += 1;
					} break;
					case KeyMessage::KMK_LEFT :
					{
						leftRight -= 1;
					} break;
					case KeyMessage::KMK_UP :
					{
						up = true;
						if(!jump)
						{
							upVel = -4;
							jump = true;
						}
					} break;
					case KeyMessage::KMK_DOWN :
					{
						//upDown += 1;
					} break;
					default: break;
				}
			} break;
			case KeyMessage::KMT_KEYUP :
			{
				switch(ke->getMessage().getKey())
				{
					case KeyMessage::KMK_RIGHT :
					{
						leftRight -= 1;
					} break;
					case KeyMessage::KMK_LEFT :
					{
						leftRight += 1;
					} break;
					case KeyMessage::KMK_UP :
					{
						up = false;
					} break;
					case KeyMessage::KMK_DOWN :
					{
						//upDown -= 1;
					} break;
					default: break;
				}
			} break;
		}
	}

	if(e->getType() == IEvent::ET_COLLISION)
	{
		//Event<CollisionMessage>* ce = (Event<CollisionMessage>*) e;
		//GameObject* src = e->getSource();
		//const Rect col = ((Event<CollisionMessage>*)((IEvent*)e))->getMessage().getRect();
		const Rect col = ((shared_ptr<Event<CollisionMessage> >) e)->getMessage().getRect();

		//float diff;

		//diff = col.x() - getRect().x();

		if(col.x() - getRect().x() > 1)
		{
			setRelPos(vector<2,float>(getRelPos()[0] - col.w(), getRelPos()[1]));
		}
		else
		if((col.x() + col.w()) - (getRect().x() + getRect().w()) < -1)
		{
			setRelPos(vector<2,float>(getRelPos()[0] + col.w(), getRelPos()[1]));
		}

		//diff = col.y() - getRect().y();

		if(col.y() - getRect().y() > 1)
		{
			jump = false;
			upVel = (upVel > 0)? 0: upVel;
			setRelPos(vector<2,float>(getRelPos()[0], getRelPos()[1] - col.h()));
		}
		else
		if((col.y() + col.h()) - (getRect().y() + getRect().h()) < -1)
		{
			upVel = (upVel < 0)? 0: upVel;
			setRelPos(vector<2,float>(getRelPos()[0], getRelPos()[1] + col.h()));
		}
	}
}

#ifndef _NO_NAMESPACE
}
#endif
